﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MakeItWeb.Framework;
using System.Web.Security;

namespace AllNations.Controls
{
    public enum UserPageMenuItem
    {
        MyProfile,
        RotatingImageManager,
        Sitemap,
        SmallgroupManager,
        Announcement,
        MemberManager
    }

    public partial class UserPageLeftNav : MakeItWebUserControl
    {
        private UserPageMenuItem _SelectedMenuItem;

        public UserPageMenuItem Selected
        {
            get { return _SelectedMenuItem; }
            set { _SelectedMenuItem = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString.ToString()))
            {
                var item = radMenuLeftNav.Items.FindItemByValue(Request.QueryString.ToString());
                if (item.Value == "Logout")
                {
                    Session.Abandon();
                    FormsAuthentication.SignOut();
                    Response.Redirect("~/Home", true);
                    return;
                }
            }
            // sitemap
            var sitemapMenu = radMenuLeftNav.Items.FindItemByValue("Sitemap");
            sitemapMenu.Visible = IsInRole("SystemAdministrator");
            // rotating image manager
            var imagemanaerMenu = radMenuLeftNav.Items.FindItemByValue("RotatingImageManager");
            imagemanaerMenu.Visible = IsInRole("HomepageImageManager");
            // small group manager
            var smallgroupManageMenu = radMenuLeftNav.Items.FindItemByValue("SmallgroupManager");
            smallgroupManageMenu.Visible = IsInRole("Administrator");
            // member manager
            var memberManageMenu = radMenuLeftNav.Items.FindItemByValue("MemberManager");
            memberManageMenu.Visible = IsInRole("Administrator");
            
            var selectedItem = radMenuLeftNav.Items.FindItemByValue(Selected.ToString());
            if (selectedItem != null)
                selectedItem.Selected = true;
        }

        protected void radMenuLeftNav_ItemClick(object sender, Telerik.Web.UI.RadMenuEventArgs e)
        {
            if (e.Item.Value == "Logout")
            {
                Session.Abandon();
                FormsAuthentication.SignOut();
                Response.Redirect("~/Home", true);
                return;
            }
        }
    }
}